#pragma once
#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<windows.h>
#include<math.h>


struct list
{
    int val;
    struct list* next;
};

struct list* CreatNode(int value)
{
    struct list* tmp = (struct list*)malloc(sizeof(struct list));
    tmp->val = value;
    tmp->next = NULL;
    return tmp;
}

struct list* CreatList(int n)
{
    struct list* head = NULL, * tail = NULL;
    for (int i = 1; i <= n; ++i)
    {
        struct list* tmp = CreatNode(i);
        if (!head)
        {
            head = tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
    tail->next = head;
    return head;
}

int main()
{
    int n = 0, m = 0;
    scanf("%d%d", &n, &m);
    struct list* cur = CreatList(n);
    int num = 1;
    while (cur->next != cur)
    {
        if (num == m)
        {
            struct list* freenode = cur->next;
            cur->val = cur->next->val;
            cur->next = cur->next->next;
            free(freenode);
            num = 0;
        }
        else
        {
            cur = cur->next;
        }
        ++num;
    }
    printf("%d\n", cur->val);
    return 0;
}